<template>
  <div>
    <!-- 基本信息 -->
    <jn-prj-detail :prj-id="urlParams.prjId" />
    <!-- 站址信息 -->
    <jn-site-detail v-if="[14].includes(+detailData.secondaryType)" :prj-id="urlParams.prjId" />
    <!-- 专属信息 -->
    <jn-proprietary-detail :prj-id="urlParams.prjId" :secondary-type="detailData.secondaryType" />
    <pos-title-materials-employ-detail
      ref="PosTitleMaterialsEmployDetail"
      :select-list="selectList"
      :form.sync="detailData"
      @doNextParents="handleDoNext"
    />
    <!-- 附件 -->
    <zr-attachmetl prj-stage="Q" />
    <!-- 审批 -->
    <jn-task-detail :process-inst-id="urlParams.processInstId" />
    <footer-button v-if="urlParams.type === 'todo'" :button-disable="buttonDisable" @handleClose="clonePage" @handleNext="handleNext" @handleBack="handleBack" />
    <next-page
      v-if="nextPageVisible"
      ref="nextPage"
      :visible.sync="nextPageVisible"
      :activity-inst-id.sync="workFlowData.activityInstID"
      :process-inst-id.sync="workFlowData.processInstID"
      :activity-def-id.sync="workFlowData.activityDefID"
      :process-def-name.sync="workFlowData.processDefName"
      :route="route"
      @doNextEntity="nextPageRes"
    />
    <Back-page
      v-if="backPageVisible"
      ref="backPage"
      :visible.sync="backPageVisible"
      :process-inst-id="workFlowData.processInstID"
      :act-inset-id="workFlowData.actInsetId"
      :route="route"
      @doReturn="backPageRes"
    />
  </div>
</template>

<script>
import NextPage from '@/components/nextPerson/NextPage'
import BackPage from '@/components/nextPerson/BackPage'
import JnPrjDetail from '@/components/jn/JnPrjDetail.vue'
import JnSiteDetail from '@/components/jn/JnSiteDetail.vue'
import JnProprietaryDetail from '@/components/jn/JnProprietaryDetail.vue'
import JnTaskDetail from '@/components/jn/JnTaskDetail.vue'
import ZrAttachmetl from '@/components/zr/ZrAttachmetl.vue'
import FooterButton from '@/components/footer/details'
import PosTitleMaterialsEmployDetail from './pos/posTitleMaterialsEmployDetail'
import { clonePage, parseParams } from '@/utils'
// import * as Common from '@/api/common'
// import * as Axios from '@/api/settlement'
import store from '@/store'

export default {
  name: 'SettlementDetail',
  components: {
    NextPage,
    BackPage,
    FooterButton,
    PosTitleMaterialsEmployDetail,
    JnPrjDetail,
    JnSiteDetail,
    JnProprietaryDetail,
    JnTaskDetail,
    ZrAttachmetl
  },
  data() {
    return {
      templateId: '',
      nextPageVisible: false, // 下一步
      backPageVisible: false, // 回退
      buttonDisable: ['doNext', 'close'],
      selectList: {},
      detailData: {},
      workFlowData: {
        activityInstID: null,
        processInstID: null, // 下一步 && 回退
        activityDefID: null,
        actInsetId: null, // 下一步 && 回退
        processDefName: ''
      },
      refbeClDetail: {}, // 获取决算竣工信息
      basicList: {}, // 基本信息
      siteInfoList: {},
      lcList: null,
      urlParams: {
        type: 0, // todo:我的待办跳转
        nodeNameWord: '',
        prjId: '',
        adjustId: '',
        activityId: '',
        workItemId: '',
        procInstId: '',
        taskId: '',
        taskType: '',
        userId: '',
        orgId: '',
        proId: '',
        city: '',
        activityInstName: '项目结算审核',
        bizSeq: '',
        nodeId: '',
        supplierCode: null,
        processInstId: ''
      },
      route: '',
      junNanUrl: process.env.VUE_APP_JUNNAN_IFRAME_URL,
      fileUrl: process.env.VUE_APP_ZHONGRUAN_URL
    }
  },
  async created() {
    // const urlData = parseParams(window.location.hash)
    // this.urlParams = {
    //   ...this.urlParams,
    //   ...urlData,
    //   taskType: urlData.type
    // }

    // if (+this.urlParams.type !== 1) {
    //  await this.getDetailByWorkId()
    // } else {
    //   const { data: nData } = await Axios.queryCurrentNodeUser(this.urlParams.processInstId)
    //   if (nData.workItemId) {
    //     this.urlParams.workItemId = nData.workItemId
    //     await this.getDetailByWorkId()
    //   } else {
    //     const { data } = await Axios.queryAdjustInfo({ prjId: this.urlParams.prjId })
    //     console.log('-****-', nData)
    //     this.urlParams.adjustId = data.adjId
    //   }
    //   // const { data } = await Axios.queryAdjustInfo({ prjId: this.urlParams.prjId })
    //   // console.log('-****-', nData)
    //   // this.urlParams.adjustId = data.adjId
    // }
    // const res = await Axios.getPrjDetail({ prjId: this.urlParams.prjId })
    // console.log(res, '___RESRSER')
    // // 获取塔类类型和室分类型
    // const towerDetail = await Axios.getTowerRoomType({ prjId: this.urlParams.prjId })
    // this.urlParams.roomType = towerDetail.data.roomType
    // this.urlParams.towerType = towerDetail.data.towerType
    // for (const i in res.data) {
    //   this.$set(this.detailData, i, res.data[i])
    // }
    // store.commit('settings/setPageTitle', res.data.prjName)
    // this.$refs['PosTitleMaterialsEmployDetail'].getParentData({ ...this.detailData, urlParams: this.urlParams })
    // if (this.urlParams.nodeNameWord === 'quantityAdjust_002') {
    //   const resButton = await Axios.isShowButton({ adjustId: this.urlParams.adjustId })
    //   if (+resButton.data === 1) {
    //     this.buttonDisable.push('back')
    //   }
    // } else if (['quantityAdjust_003', 'quantityAdjust_004', 'quantityAdjust_005'].includes(this.urlParams.nodeNameWord)) {
    //   this.buttonDisable.push('back')
    // }
  },
  mounted() {
    this.getSelectData()
  },
  methods: {
      clonePage,
    async getDetailByWorkId() {
      // const user = await Axios.getUserInfo()
      // const { data } = await Axios.getWFWorkItem({ workItemId: this.urlParams.workItemId })
      // this.lcList = data
      // this.urlParams.prjId = data.bizObject.prjId
      // this.urlParams.processInstId = data.processInstID
      // this.urlParams.adjustId = data.bizObject.bizSeq
      // this.urlParams.nodeNameWord = data.activityDefID
      // this.urlParams.activityId = data.activityDefID
      // this.urlParams.taskId = data.workItemId
      // this.urlParams.activityInstName = data.activityInstName
      // this.urlParams.bizSeq = data.bizObject.bizSeq
      // this.urlParams.orgId = user.data.user.departmentId
      // this.urlParams.nodeId = data.activityDefID
    },
    // 下一步
    async handleNext() {
      console.log(this.lcList, '____lcList')

      this.workFlowData = {
        activityInstID: this.lcList.activityInstID,
        processInstID: this.lcList.processInstID,
        activityDefID: this.lcList.activityDefID,
        processDefName: this.lcList.processInstName
      }
      // const res = await this.$refs['PosTitleMaterialsEmployDetail'].doNext()
      // if (!res) return
      // const response = await this.$refs['PosTitleMaterialsEmployDetail'].doNextStep('')
      // if (!response) return
      // const params = {
      //   workItemId: this.urlParams.workItemId,
      //   workFlowFlag: 'doNext' // 下一步校验
      // }
      // const res = await Axios.flowOperationCheckApi(params)
      // if (+res.code !== 200) {
      //   return
      // }
      // this.nextPageVisible = true
    },
    handleDoNext(value) {
        this.templateId = value.params?.subProcDefName?.templateId
      if (+value.ifFlag === 1 && +value.params.isNewRailWay !== 1 && this.urlParams.nodeNameWord === 'quantityAdjust_001') {
        this.route = '1'
      }
      if (this.urlParams.nodeNameWord === 'quantityAdjust_002') {
        if (+value.ifFlag === 1 && +value.params.isNewRailWay !== 1 && this.urlParams.nodeNameWord === 'quantityAdjust_002') {
          this.route = '2'
        }
        if (+value.params.isNewRailWay === 1) {
          this.route = '3'
        }
      }
      if (+value.ifFlag === 1 && this.urlParams.nodeNameWord === 'quantityAdjust_003') {
        this.route = '4'
      }
      this.nextPageVisible = true
    },
    // 回退
    async handleBack() {
      // const res1 = await this.$refs['PosTitleMaterialsEmployDetail'].doNextStep('BACK')
      // this.workFlowData = {
      //   activityInstID: this.lcList.activityInstID,
      //   processInstID: this.lcList.processInstID,
      //   actInsetId: this.lcList.activityInstID,
      //   activityDefID: this.lcList.activityDefID,
      //   processDefName: this.lcList.processInstName
      // }
      // const params = {
      //   workItemId: this.urlParams.workItemId,
      //   workFlowFlag: 'doBack' // 回退校验
      // }
      // const res = await Axios.flowOperationCheckApi(params)
      // if (+res.code !== 200) {
      //   return
      // }
      // this.backPageVisible = true
      // console.log(res1)
    },
    // 下一步确认回调
    async nextPageRes(item) {
      const ifNextVal = '1' // 下一步传 1
      this.nextBackPageRes(item, ifNextVal)
    },
    // 退回确认回调
    async backPageRes(item) {
      const ifNextVal = '2' // 回退传2
      this.nextBackPageRes(item, ifNextVal)
    },
    // 下一步 && 回退
    async nextBackPageRes(item, ifNextVal) {
      console.log(item, ifNextVal, 'ifNextVal')
      var workFlowFlag = ''
      // var messageVal = ''
      if (ifNextVal === '1') {
        workFlowFlag = 'doNext'
        // messageVal = '提交成功'
      } else {
        workFlowFlag = 'doBack'
        // messageVal = '回退成功'
      }
      // 2023-10-16 暂时不需要调用此接口
      // var data = {
      //   prjType: this.detailData.prjType,
      //   ifProject: 'Y',
      //   prjSubType: this.detailData.prjType === 'M' ? this.detailData.secondaryType : null, // 接口返回
      //   manageMode: this.detailData.manageMode, // 接口返回
      //   wfStage: 'Q',
      //   businessMajor: '05'
      // }
      // const getSceneTempRela = await Axios.getSceneTempRela(data)
      // if (+getSceneTempRela.code !== 200) return
      const obj = {
        prjId: this.urlParams.prjId,
        workFlowFlag: workFlowFlag,
        // 2023-10-16 暂时不需要传递此参数
        templateId: this.templateId,
        nextNodeId: item.nodeId,
        workItemId: this.urlParams.workItemId,
        opinion: item.opinion,
        userIds: item.empId,
        nextSceneId: item.sceneId,
        adjustId: this.urlParams.adjustId,
        route: this.route,
        ifTwo: '1',
        detailId: item.detailId,
        nextNodeName: item.nodeName
      }
      // const doNext = await Axios.doNext(obj)
      // if (+doNext.code === 200) {
      //   clonePage()
      //   // this.$confirm(messageVal, {
      //   //   confirmButtonText: '确定',
      //   //   type: 'success',
      //   //   showClose: false,
      //   //   showCancelButton: false
      //   // }).then(() => {
      //   //   clonePage()
      //   //   console.log('messageVal')
      //   // })
      // }
    },
    // 获取下拉框数据
    async getSelectData() {
      const arr = ['PROJ_TYPE', 'SECONDARY_TYPE', 'BUILD_MODES', 'PURCHASE_METHOD']
      // for (const i in arr) {
      //   const res = await Common.getDropdownData(arr[i])
      //   this.$set(this.selectList, arr[i], res.data)
      // }
    }

  }
}
</script>
